using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;
using System.Data.Linq.SqlClient;

namespace VoiceAd.DAL
{
    public class LogRepository : IlogRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public LogRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblLog> GetListLog()
        {
            return context.tblLogs.ToList();
        }

        public IEnumerable<tblLog> GetByUserNameLog(string UserName)
        {
            IEnumerable<tblLog> lst = from a in context.tblLogs
                                      where SqlMethods.Like(a.UserName, "%" + UserName + "%")
                                      orderby a.CreateDate descending
                                      select a;

            return lst;
        }

        public void InsLog(tblLog tbl)
        {
            context.tblLogs.InsertOnSubmit(tbl);
            context.SubmitChanges();
        }
        public void UpdLog(tblLog tbl)
        {
            tblLog obj = context.tblLogs.Where(x => x.Id == tbl.Id).First();
            obj.UserName = tbl.UserName;
            obj.CreateDate = tbl.CreateDate;
            obj.ActionName = tbl.ActionName;
            obj.Description = tbl.Description;
            context.SubmitChanges();
        }

        public void DelLog(int id)
        {
            tblLog obj = context.tblLogs.Single(x => x.Id == id);
            context.tblLogs.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
